package com.jinlejia.erp.ui.mine.process;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityOperationDetailsBinding;
import com.jinlejia.erp.ui.mine.contract.MyContractDetailsActivity;
import com.jinlejia.erp.utils.DataFilter;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 我的办件流程详情（Activity）
 */

public class ProeessDetailActivity extends BaseActivity {
    private JSONObject jsonObject = new JSONObject();
    private ActivityOperationDetailsBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_operation_details);
        binding.backBtn.setOnClickListener(view -> finish());

        getData();
    }


    /**
     * 获取数据
     */
    private void getData() {
        jsonObject.put("id", getIntent().getStringExtra("id"));
        jsonObject.put("contractCode", getIntent().getStringExtra("contractCode"));
        OkHttpUtils
                .postString()
                .url(ApiConfig.MY_PROCESS_DETAILS)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(ProeessDetailActivity.this, response, null)) {
                            final JSONObject data = JSON.parseObject(response.get("data").toString());
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
                            binding.operationContractNumber.setText(data.get("contractCode").toString());
                            binding.operationTurnoverTime.setText(sdf.format(new Date(Long.valueOf(data.get("tradeTime").toString()))));
                            binding.operationHouseProperty.setText(data.get("houseProperty").toString());
                            binding.operationPropertyAddress.setText(data.get("address").toString());
                            binding.operationOwner.setText(data.get("owner").toString());
                            binding.operationCustomer.setText(data.get("customerName").toString());
                            binding.operationScheduleName.setText(data.get("stepName").toString());
                            switch (data.get("schedule").toString()) {
                                case "0":
                                    binding.operationScheduleState.setText("未完成");
                                    binding.operationEstimatedTime.setText(sdf.format(new Date(Long.valueOf(data.get("expectTime").toString()))));
                                    break;
                                case "1":
                                    binding.operationScheduleState.setText("已完成");
                                    binding.operationEstimatedTime.setText(sdf.format(new Date(Long.valueOf(data.get("expectTime").toString()))));
                                    binding.operationActualTime.setText(sdf.format(new Date(Long.valueOf(data.get("finishTime").toString()))));
                                    binding.operationAssignPeople.setText(data.get("nextName").toString());
                                    binding.operationRemarksEdit.setText(data.get("remark").toString());
                                    binding.operationPresentPeople.setText(data.get("finishUser").toString());
                                    break;
                            }
                            binding.operationContractNumber.setOnClickListener(view -> {
                                switch (data.get("pactState").toString()) {
                                    case "0":
                                        Intent intent = new Intent(ProeessDetailActivity.this, MyContractDetailsActivity.class);
                                        intent.putExtra("contractCode", data.get("contractCode").toString());
                                        startActivity(intent);
                                        break;
                                    case "1":
                                        CustomToast.showToast(ProeessDetailActivity.this, "成交人不是自己，无法查看", 2000);
                                        break;
                                }
                            });
                        }
                    }
                });
    }
}
